using System;
using SCG.SolarVengeanceEngine.StarShips;
using System.Collections.Generic;

namespace SCG.SolarVengeanceEngine.BrainInfo
{
    public class StarShipInfo : SVObjectInfo
    {
        //constructor
        public StarShipInfo(StarShip ship)
            : base(ship)
        {
            _ship = ship;
        }

        //Has it been identified?
        public bool IdentifiedByMe
        {
            get
            {
                return _ship.DiscoveredBy[Brain.ThisPlayer];
            }
        }

        //Is it owned by the brain?
        public bool OwnedByMe
        {
            get
            {
                return _ship.Owner == Brain.ThisPlayer;
            }
        }

        //StarShip type
        public StarShipType ShipType
        {
            get
            {
                if (IdentifiedByMe)
                {
                    if (OwnedByMe || _ship.ShipType != StarShipType.DoppleShip)
                        return _ship.ShipType;
                    else
                        return StarShipType.WarShip;
                }
                else
                    return StarShipType.WarShip;
            }
        }

        //Is it set to attack adjacent enemies?
        public bool AttackAdjacent
        {
            get
            {
                if (OwnedByMe)
                    return _ship.AttackAdjacent;
                else
                    return false;
            }
        }

        //Cargo held
        public int Resources
        {
            get
            {
                if (IdentifiedByMe)
                {
                    if (_ship is ResourceStarShip)
                    {
                        ResourceStarShip rss = _ship as ResourceStarShip;
                        return rss.ResourcesInt;
                    }
                    else
                        return 0;
                }
                else
                    return 0;
            }
        }

        //Is it cloaked?
        public bool Cloaked
        {
            get
            {
                if (IdentifiedByMe)
                    return _ship.Cloaked;
                else
                    return false;
            }
        }

        //Return the 3 digit code
        public string Code
        {
            get
            {
                return _ship.Code;
            }
        }

        //Return the MapObject it is dragging
        public SVMapObject Dragging
        {
            get
            {
                if (OwnedByMe)
                {
                    if (_ship is GravShip)
                    {
                        GravShip grav = _ship as GravShip;
                        return grav.Dragging;
                    }
                    else
                        return null;
                }
                else
                    return null;
            }
        }

        //Return Weapons + CommandInfluence
        public int EffectiveWeapons
        {
            get
            {
                if (OwnedByMe)
                {
                    if (_ship is WarShip)
                    {
                        WarShip warShip = _ship as WarShip;
                        return warShip.EffectiveWeapons;
                    }
                    else
                        return 0;
                }
                else
                    return 0;
            }
        }

        //Return Engines
        public int Engines
        {
            get
            {
                if (OwnedByMe)
                    return _ship.Engines;
                else if (IdentifiedByMe)
                {
                    if (_ship.ShipType == StarShipType.DoppleShip)
                        return (_ship as DoppleShip).DoppleEngines;
                    else                  
                        return _ship.Engines;
                }
                else
                    return 0;
            }
        }

        //Return primary system value
        public int Value
        {
            get
            {
                if (OwnedByMe)
                    return _ship.Value;
                else if (IdentifiedByMe)
                {
                    if (_ship.ShipType == StarShipType.DoppleShip)
                        return (_ship as DoppleShip).DoppleValue;
                    else
                        return _ship.Value;
                }
                else
                    return 0;
            }
        }

        //health rating
        public double Health
        {
            get
            {
                if (OwnedByMe)
                    return _ship.Health;
                else if (IdentifiedByMe)
                {                 
                    int max = _ship.MaxEngines + _ship.MaxValue;
                    int current = _ship.Engines + _ship.Value;
                    if (max <= 0)
                        return 0;
                    return (int)((current * 100) / max);
                }
                else
                    return 0;
            }
        }

        //Return damaged
        public bool IsDamaged
        {
            get
            {
                if (IdentifiedByMe)
                    return _ship.IsDamaged;
                else
                    return false;
            }
        }

        //can I attack StarSystems?
        public bool CanTargetStarSystems
        {
            get
            {
                if (OwnedByMe)
                    return _ship.CanConquerStarSystems;
                else
                    return false;
            }
        }

        //Return the known weapons rating
        public int KnownWeapons
        {
            get
            {
                if (OwnedByMe)
                    return _ship.Value;
                else if (IdentifiedByMe)
                {
                    if (_ship is DoppleShip)
                        return (_ship as DoppleShip).DoppleValue;
                    else
                        return _ship.Value;
                }
                else
                {
                    if (_ship is DoppleShip)
                        return (_ship as DoppleShip).GetDoppleKnownWeapons(Brain.ThisPlayer);
                    else if (_ship is WarShip)
                        return (_ship as WarShip).KnownWeapons[Brain.ThisPlayer];
                    else
                        return 0;
                }
            }
        }

        //Return max engines/value
        public int MaxEngines
        {
            get
            {
                if (OwnedByMe)
                    return _ship.MaxEngines;
                else if (IdentifiedByMe)
                {
                    if (_ship is DoppleShip)
                        return (_ship as DoppleShip).DoppleMaxEngines;
                    else                    
                        return _ship.MaxEngines;
                }
                else
                    return 0;
            }
        }
        public int MaxValue
        {
            get
            {
                if (OwnedByMe)
                    return _ship.MaxValue;
                else if (IdentifiedByMe)
                {
                    if (_ship is DoppleShip)
                        return (_ship as DoppleShip).DoppleMaxValue;
                    else                  
                        return _ship.Value;
                }
                else
                    return 0;
            }
        }

        //Owner's name
        public string Owner
        {
            get
            {
                return _ship.Owner.Name;
            }
        }

        //Index of owner
        public int OwnerInt
        {
            get
            {
                if (_ship.Owner == null)
                    return -1;
                else
                    return _ship.Owner.Index;
            }
        }

        //Was it tagged by me?
        public bool TaggedByMe
        {
            get
            {
                return _ship.TaggedBy[Brain.ThisPlayer];
            }
        }

        //Tugging
        public StarShipInfo Tugging
        {
            get
            {
                if (OwnedByMe)
                {
                    if (_ship is Tug)
                    {
                        Tug tug = _ship as Tug;
                        return tug.Towing.Info;
                    }
                    else
                        return null;             
                }
                else
                    return null;
            }
        }
        public StarShipInfo TuggedBy
        {
            get
            {
                if (OwnedByMe)
                {
                    foreach(StarShip ship in Brain.ThisGame.StarShips)
                        if (ship.DiscoveredBy[Brain.ThisPlayer])
                            if (ship is Tug)
                            {
                                Tug tug = ship as Tug;
                                if (tug.Towing == _ship)
                                    return tug.Info;
                            }
                    return null;
                }
                else
                    return null;
            }
        }

        //Cloakarrows
        public IList<int> CloakArrows
        {
            get
            {
                if (OwnedByMe && ShipType == StarShipType.CloakDetector)
                    return (_ship as CloakDetector).CloakArrows;
                else
                    return null;
            }
        }

        //The Order (if any)
        public OrderType Order
        {
            get
            {
                if (OwnedByMe)
                {
                    if (_ship.Order != null)
                        return _ship.Order.OrderType;
                    else
                        return OrderType.None;
                }
                else
                    return OrderType.None;
            }
        }

        //UDOs being carried
        public IList<UDOInfo> UDOs
        {
            get
            {
                List<UDOInfo> ul = new List<UDOInfo>();
                if (OwnedByMe)
                    foreach (UDO udo in _ship.UDOs)
                        ul.Add(udo.Info);
                return ul;
            }
        }

        //private members
        internal StarShip _ship;
    }
}
